$(function () {
     var roleId = $(':input[name=roleId]').val();
     var deptId = $(':input[name=deptId]').val();
    $(".role-tree").tree({ //设置角色树
        url : ROOT_PATH+"/permission/dataAuthority/selectDataAuthority.shtml?deptId="+deptId+"&roleId="+roleId,
        checkbox:true,
        lines:true,
       // cascadeCheck:true, //禁止联动选择
        onCheck:function(node,checked){
            var tt = $(".role-tree");
            if(checked)
            {
                var childNodeChecked = tt.tree("getChildren",node.target);
                for(var i= 0;i<childNodeChecked.length;i++)
                {
                    tt.tree("check", childNodeChecked[i].target);
                }
            }
            else
            {
                var childNodeUnChecked = tt.tree("getChildren",node.target);
                for(var j= 0;j<childNodeUnChecked.length;j++)
                {
                    tt.tree("uncheck", childNodeUnChecked[j].target);
                }
            }

        }
    });
    //获取数据权限关联部门id
    var getDataAuthorityDepartmentArray = function () {
        var newDataAuthorityArray = [];
        $('.role-tree').each(function () {
            var checked = $(this).tree('getChecked');
            var dataAuthority = $.map(checked, function (item) {
                return item.id;
            });
            newDataAuthorityArray.push(dataAuthority);
        });
        return newDataAuthorityArray;
    };

    $('#btn_save').on("click", function () {
        $.messager.progress();	// 显示进度条
        var dataDeptIdArray = getDataAuthorityDepartmentArray();

        $('#form_user').form('submit', {
            url: ROOT_PATH + '/permission/dataAuthority/editDataAuthority.shtml',
            queryParams:{
                dataDeptIdArray:dataDeptIdArray
            },
            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
                }
                return isValid;	// 返回false终止表单提交
            },
            success: function (result) {
                try {
                    result = $.parseJSON(result);
                }catch (e) {
                    $.messager.progress('close');
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
                if (result.success){
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:1},function () {
                        parent.$('#dg1').datagrid('load');
                        parent.layer.closeAll(); //先得到当前iframe层的索引
                    });
                }else {
                    $.messager.progress('close');	// 如果提交失败则不做处理
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });
});